package com.erpanderp.wam.usm.view.user;

import java.io.Serializable;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.context.RequestContext;

import com.erpanderp.wam.usm.model.entity.complaintandsuggestion.ComplaintAndSuggestion;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.user.ComplaintsAndSuggestionDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ViewScoped
@ManagedBean
public class RaiseComplaintBean implements Serializable{

	private static final long serialVersionUID = 1L;
	
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	private WAMUser wamUser;
	
	private ComplaintAndSuggestion createComplaint;
	private boolean FORM_VALID;
	private ComplaintAndSuggestion complaintsAndSuggestion = new ComplaintAndSuggestion();
	ComplaintsAndSuggestionDAO complaintsAndSuggestionDAO = new ComplaintsAndSuggestionDAO();
	private String complaintCode = null;
	boolean complaintExists = true;

	public RaiseComplaintBean(){
		setWamUser((WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER));
		
		complaintCode=CommonLookup.getAutoGeneratedComplaintCode();
		
	}
	
	public String saveComplaint(){	
		validateForm();
		if(FORM_VALID){
			complaintExists = complaintsAndSuggestionDAO.isComplaintCodeExistOrNot(complaintCode);
			
			if (!complaintExists){                  
				complaintCode=CommonLookup.getAutoGeneratedComplaintCode();								
			}	
			
			complaintsAndSuggestion.setCode(complaintCode);
			complaintsAndSuggestion.setCreateDate(new Date());
			complaintsAndSuggestion.setStatus("ACTIVE");
			complaintsAndSuggestion.setEmployee(wamUser.getEmployee());		
			complaintsAndSuggestion.setDateRaised(new Date());
			complaintsAndSuggestion.setCreatedBy(wamUser.getEmployee().getFirstName()+" "+wamUser.getEmployee().getLastName());
			complaintsAndSuggestion.setDescription("Developer");
			complaintsAndSuggestionDAO.createComplaintAndSuggestion(complaintsAndSuggestion);
			
			complaintsAndSuggestion = new ComplaintAndSuggestion();
			complaintCode=CommonLookup.getAutoGeneratedComplaintCode();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,  " Your complaint/suggestion created successfully.", ""));
			return null;
			//RequestContext.getCurrentInstance().execute("successDialog.show();");
			
			//return "raisecomplaint";
			
		}else{
			System.out.println("AT CALLER METHOD: FORM IN VALID");  
			return null;
		}
		
	}
	
	public void confirmationAction(ActionEvent e){
		validateForm();	
		if(FORM_VALID){	
			RequestContext.getCurrentInstance().execute("confirmation.show();");
		}else {
			RequestContext.getCurrentInstance().execute("confirmation.hide();");
		}
    }	
	
	
	private void validateForm(){
		FORM_VALID = true;
		
	   if(ValidationUtil.isEmpty(complaintsAndSuggestion.getComplaint(), "Complaint/Suggestion", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		System.out.println("Finally FORM_VALID is :::::"+FORM_VALID);
		
	}

	//GENERATE SETTERS AND GETTERS
	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public ComplaintAndSuggestion getCreateComplaint() {
		return createComplaint;
	}

	public void setCreateComplaint(ComplaintAndSuggestion createComplaint) {
		this.createComplaint = createComplaint;
	}

	public ComplaintAndSuggestion getComplaintsAndSuggestion() {
		return complaintsAndSuggestion;
	}

	public void setComplaintsAndSuggestion(ComplaintAndSuggestion complaintsAndSuggestion) {
		this.complaintsAndSuggestion = complaintsAndSuggestion;
	}

	public String getComplaintCode() {
		return complaintCode;
	}

	public void setComplaintCode(String complaintCode) {
		this.complaintCode = complaintCode;
	}


}
